package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.model.Customer;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.MainView;
import group2assignone.view.UITool;
import group2assignone.view.customer.CustomerWelcomeView;
import javax.swing.JOptionPane;

/**
 * Use Case: Delete Customer Account
 * Pre-Condition: customer selects 'delete account' from the customer 
 * welcome view
 * Post-Condition: UI is updated
 * 1. System requests confirmation from user to delete the account
 * 2. System deletes the account
 * 3. System notifies the user that the account has been deleted
 * 4. System updates the UI to the main menu
 * 2a. - System cannot access account to delete it (connection error etc)
 *     - System displays error message
 *     - System updates UI to CustomerWelcomeView
 * @author group2
 */
public class UCDeleteCustomerAccount implements UCController {

    private Customer cust;
    private CustomerWelcomeView view;
    private boolean okDAO;
    private int option;
    private MyDAO coms;
    private MainView view2;

    /**
     * sets the customer and view, used for personalisation
     * @param cust
     * @param view
     */
    public UCDeleteCustomerAccount(Customer cust, CustomerWelcomeView view) {
        this.cust = cust;
        this.view = view;
        coms = UCStartup.getInstance().getDAO();
        view2 = new MainView();
    }

    /**
     *Use Case Controller - allows the user to delete their account once
     * they are logged in.
     */
    public void run() {
        //1. System requests confirmation from user to delete the account
        option = view.confirmDelete();
        if (option == JOptionPane.YES_OPTION) {
            //2. System deletes the account
            okDAO = coms.sQLDeleteCustomer(cust.getUsername());
            if (okDAO) {
                //3. System notifies the user that the account has been deleted
                view.notifyDelete();
                //4. System updates the UI to the main menu
                view2.go();
                view.dispose();
            } else {//2a. - System cannot access account to delete it (connection error etc)
                //System displays error message
                UITool.displayError("Cannot connect to database at this time. Try again later.", view);
            }
        }
    }
}
